<?php
/**
 * 系统用户管理控制器
 * User: Administrator
 * Date: 2018/10/29 0029
 * Time: 16:07
 */
namespace app\admin\controller;

use app\admin\model\SystemUser;
use service\DataService;
use think\Db;
class User extends BasicAdmin
{

    /**
     * 指定当前数据表
     * @var string
     */
    public $table = 'SystemUser';

    /**
     * 用户列表
     */
    public function index()
    {

        if($this->request->isPost()){
            $params = $this->request->param();
            $db = Db::name($this->table)->where('delete_at', 0);
            return parent::_list($db,$params);
        }
        $this->title = '网站配置 > 系统用户';
        return $this->fetch('', ['title' => $this->title]);

    }

    protected function _index_data_filter(&$data){
        $authos = Db::name('SystemAuth')->column('id,title');
        foreach ($data as &$vo) {
            $vo['quanxian'] = $authos[$vo['authorize']];
        }
    }
    /**
     * 授权管理
     * @return array|string
     */
    public function auth()
    {
        return $this->_form($this->table, 'auth');
    }

    /**
     * 用户添加
     */
    public function add()
    {
        return $this->_form($this->table, 'form');
    }

    /**
     * 用户编辑
     */
    public function edit()
    {
        return $this->_form($this->table, 'form');
    }

    /**
     * 用户密码修改
     */
    public function pass()
    {
        if ($this->request->isGet()) {
            $this->assign('verify', false);
            return $this->_form($this->table, 'pass');
        }
        $post = $this->request->post();
        if ($post['password'] !== $post['repassword']) {
            $this->error('两次输入的密码不一致！');
        }
        $data['id'] = $post['id'];
        SystemUser::createPass($post['password'], $data);
        if (DataService::save($this->table, $data, 'id')) {
            $this->result([],200,'密码修改成功，下次请使用新密码登录！','json');
        }
        $this->error('密码修改失败，请稍候再试！');
    }

    /**
     * 表单数据默认处理
     * @param array $data
     */
    public function _form_filter(&$data)
    {
        if ($this->request->isPost()) {
            if (isset($data['authorize']) && is_array($data['authorize'])) {
                $data['authorize'] = join(',', $data['authorize']);
            }
            if (isset($data['id'])) {
                unset($data['username']);
            } elseif (Db::name($this->table)->where(['username' => $data['username']])->count() > 0) {
                $this->error('用户账号已经存在，请使用其它账号！');
            }

            if(isset($data['id']) && !empty($data['id'])){
                !isset($data['update_at']) && $data['update_at'] = date('Y-m-d H:i:s');
            }else {
                //生成默认密码
                !isset($data['password']) && SystemUser::createPass('123456', $data);
                !isset($data['create_at']) && $data['create_at'] = date('Y-m-d H:i:s');
            }
        } else {
            $data['authorize'] = explode(',', isset($data['authorize']) ? $data['authorize'] : '');
            $this->assign('authorizes', Db::name('SystemAuth')->where(['status' => '1'])->select());
            $iframe = $this->request->get('iframe');
            (isset($iframe) && !empty($iframe)) && $this->assign('iframe',$iframe);
        }
    }

    /**
     * 用户状态（1启用 2禁用）
     */
    public function status(){

        $status = $this->request->param('value');
        if(2 == $status){
            if (in_array('1', explode(',', $this->request->post('id')))){
                $this->result([],0,'系统超级账号禁止操作！','json');
            }
        }
        if (DataService::update($this->table)) {
            $msg = $status == 1 ? '用户启用成功！':'用户禁用成功！';
            $this->result(['status'=>$status],200,$msg,'json');
        }
        $msg = $status == 1 ? '用户启用失败，请稍候再试！':'用户禁用失败，请稍候再试！';
        $f_status = $status == 1 ? 2 : 1;
        $this->result(['status'=>$f_status],0,$msg,'json');
    }

    /**
     * 删除用户
     */
    public function del()
    {
        if (in_array('1', explode(',', $this->request->post('id')))) {
            $this->error('系统超级账号禁止删除！');
        }
        if (DataService::update($this->table)) {
            $this->success("用户删除成功！", '');
        }
        $this->error("用户删除失败，请稍候再试！");
    }

}
